#include "base/logging.h"
#include "dispatch_presence_task.h"

#include "base/html_escape.h"
#include "base/url_encode.h"
#include "base/curl_client.h"

#include "im/common/common.h"

#include "im/adapters/user_profile_adapter.h"
#include "im/adapters/session_adapter.h"
#include "im/adapters/message_outlet_adapter.h"
#include "im/adapters/message_history_adapter.h"
#include "im/adapters/recent_contact_adapter.h"

#include "shop_info_cache.h"
#include "notify_filter.h"

namespace imserver {

extern int g_bomb_percentage;

void DispatchPresenceTask::ShopPresenceChanged() {
}

void DispatchPresenceTask::CustomerPresenceChanged() {
  if (type_ != SessionType::WEB) {
    LOG_INFO("CustomerPresenceChanged OFFLINE, uid=" << uid_);
    return;
  }
  if (uid_ % 100 >= g_bomb_percentage) {
    LOG_INFO("CustomerPresenceChanged percentage filtered, uid=" << uid_ << '/' << g_bomb_percentage);
    return;
  }

  if (NotifyFilter::Instance().MostRecentBombed(uid_) != 0) {
    LOG_INFO("CustomerPresenceChanged MostRecentBombed, uid=" << uid_);
    return;
  }

  SessionList uid_sessions;
  SessionAdapter::Instance().GetSessionList(uid_, &uid_sessions, 2);

  ImMessage notify_message;

  static int64_t customer_guide_ids[] = {317126174, 317124696, 317121850, 317125217};
  int64_t selected_guide = customer_guide_ids[rand() % (sizeof(customer_guide_ids) / sizeof(int64_t))];
  notify_message.sender = selected_guide;
  notify_message.receiver = uid_;
  notify_message.timestamp = time(NULL);

  SetMessageType(notify_message, MSG_TYPE_OFFLINE_NOTIFY);
  std::stringstream body;

  body << "【360同城帮】系统为您找到了在线的修电脑商家，您可以<a class=\"broadcast-client\" data-shop=\""
       << selected_guide << "\" href=\"#nogo\">点击这里</a>向他们求助。";
  notify_message.body = body.str();

  for(size_t i = 0; i < uid_sessions.size(); ++i) {
    int res = MessageOutletAdapter::Instance().DistributeMessage(uid_sessions[i].key, notify_message, 2);
    LOG_INFO("bomb message type=" << MessageType(notify_message) << " fake_sender=" << selected_guide << " reveiver=" 
             << notify_message.receiver << " res=" << res);
  }

  return;
  //////////////////////////////////////////////////
  LOG_INFO("CustomerPresenceChanged stopped, uid=" << uid_);
  std::vector<int64_t> rand_shops;
  ShopInfoCache::Instance().RandomGetShops(5, &rand_shops);

  std::map<int64_t, SessionType::type> online_map;
  if (!rand_shops.empty()) {
    SessionAdapter::Instance().GetOnlineUsers(rand_shops, &online_map);
  }
  LOG_INFO("CustomerPresenceChanged stopped, uid=" << uid_);

  ImMessage broadcast_message;
  broadcast_message.sender = uid_;
  broadcast_message.timestamp = time(NULL);
  SetMessageType(broadcast_message, MSG_TYPE_OFFLINE_NOTIFY);
  broadcast_message.body = "【360同城帮】该客户有修电脑需求，直接回复可向他提供帮助(5分钟内抢答有效)。";
 
  for(std::map<int64_t, SessionType::type>::iterator it = online_map.begin(); it != online_map.end(); ++it) {
    SessionList sessions;
    SessionAdapter::Instance().GetSessionList(it->first, &sessions, 2);
    if (sessions.empty()) {
      continue;
    }
    broadcast_message.receiver = it->first;
    for(size_t j = 0; j < sessions.size(); ++j) {
      MessageOutletAdapter::Instance().DistributeMessage(sessions[j].key, broadcast_message, 2);
      LOG_INFO("原子弹计划消息 uid=" << uid_ << " shop=" << it->first);
    }
  }
  LOG_INFO("CustomerPresenceChanged delivered, uid=" << uid_ << " online_rand_shops=" << online_map.size() << '/' << rand_shops.size());
}

void DispatchPresenceTask::run() {
  LOG_INFO("DispatchPresenceTask begin, uid=" << uid_);

  if (ShopInfoCache::Instance().Contains(uid_)) {
    ShopPresenceChanged();
  } else {
    CustomerPresenceChanged();
  }
}

}


